package edu.bu.trident.k9scheduler.shared;

//import java.util.regex.Matcher;
//import java.util.regex.Pattern;

/**
 * <p>
 * FieldVerifier validates that the name the user enters is valid.
 * </p>
 * <p>
 * This class is in the <code>shared</code> package because we use it in both
 * the client code and on the server. On the client, we verify that the name is
 * valid before sending an RPC request so the user doesn't have to wait for a
 * network round trip to get feedback. On the server, we verify that the name is
 * correct to ensure that the input is correct regardless of where the RPC
 * originates.
 * </p>
 * <p>
 * When creating a class that is used on both the client and the server, be sure
 * that all code is translatable and does not use native JavaScript. Code that
 * is note translatable (such as code that interacts with a database or the file
 * system) cannot be compiled into client side JavaScript. Code that uses native
 * JavaScript (such as Widgets) cannot be run on the server.
 * </p>
 */
public class FieldVerifier {

	/**
	 * Verifies that the specified name is valid for our service.
	 * 
	 * In this example, we only require that the name is at least four
	 * characters. In your application, you can use more complex checks to
	 * ensure that usernames, passwords, email addresses, URLs, and other fields
	 * have the proper syntax.
	 * 
	 * @param name
	 *            the name to validate
	 * @return true if valid, false if invalid
	 */
	public static boolean isValidName(String name) {
		if (name == null) {
			return false;
		}
//
//		int index = name.indexOf(" ");
//		String username = name.substring(0, index).trim();
//		String password = name.substring(index).trim();
//
//		Pattern p = null;
//		Matcher m = null;
//
//		// make sure there is only one '@' symbol
//		int ndx_start = username.indexOf('@');
//		int ndx_end = username.lastIndexOf('@');
//		if (ndx_start == ndx_end && ndx_start != -1) {
//			// break the string into local and domain
//			String local = username.substring(0, ndx_start);
//			String domain = username.substring(ndx_start + 1);
//
//			// check local length is not greater than 64
//			if (local.length() > 64)
//				return false;
//
//			// check domain length 255
//			if (domain.length() > 255)
//				return false;
//
//			// finally check username string with the regex
//			// source code help from:
//			// http://java.sun.com/developer/technicalArticles/releases/1.4regex/
//			p = Pattern.compile("^\\.|^\\@");
//			m = p.matcher(username);
//			if (m.find())
//				return false;
//
//			p = Pattern.compile("[^A-Za-z0-9\\.\\@_\\-~#]+");
//			m = p.matcher(username);
//			if (m.find())
//				return false;
//		}
//
//		// Password must contain at least one of the following characters:
//		// abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ
//		p = Pattern.compile("[A-Za-z]");
//		m = p.matcher(password);
//		if (!m.find())
//			return false;
//
//		// Password must contain at least one of the following digits:
//		// 0123456789
//		p = Pattern.compile("[0-9]");
//		m = p.matcher(password);
//		if (!m.find())
//			return false;
//
//		// Password must contain at least one of the following punctuation:
//		// !"#$%&()''*+,-/:;<=>?_
//		p = Pattern.compile("[!\"#$%&()''*+,-/:;<=>?_]");
//		m = p.matcher(password);
//		if (!m.find())
//			return false;

		return true;
	}
}
